
/**
 * @author: wanghe02@meituan.com
 * @since: 15-10-12 下午11:45
 * @returns {MTIM:object}
 */
Object.defineProperty(exports, "__esModule", { value: true });
var b64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
var _base64 = {
    encode: function (string) {
        var result = '';
        var i = 0;
        do {
            var a = string.charCodeAt(i++);
            var b = string.charCodeAt(i++);
            var c = string.charCodeAt(i++);
            a = a ? a : 0;
            b = b ? b : 0;
            c = c ? c : 0;
            var b1 = (a >> 2) & 0x3F;
            var b2 = ((a & 0x3) << 4) | ((b >> 4) & 0xF);
            var b3 = ((b & 0xF) << 2) | ((c >> 6) & 0x3);
            var b4 = c & 0x3F;
            if (!b) {
                b3 = b4 = 64;
            }
            else if (!c) {
                b4 = 64;
            }
            result += b64map.charAt(b1) + b64map.charAt(b2) + b64map.charAt(b3) + b64map.charAt(b4);
        } while (i < string.length);
        return result;
    },
    decode: function (string) {
        var result = '';
        var i = 0;
        do {
            var b1 = b64map.indexOf(string.charAt(i++));
            var b2 = b64map.indexOf(string.charAt(i++));
            var b3 = b64map.indexOf(string.charAt(i++));
            var b4 = b64map.indexOf(string.charAt(i++));
            var a = ((b1 & 0x3F) << 2) | ((b2 >> 4) & 0x3);
            var b = ((b2 & 0xF) << 4) | ((b3 >> 2) & 0xF);
            var c = ((b3 & 0x3) << 6) | (b4 & 0x3F);
            result += String.fromCharCode(a) + (b ? String.fromCharCode(b) : '') + (c ? String.fromCharCode(c) : '');
        } while (i < string.length);
        return result;
    }
};
/**
 * utf16 to utf8
 */
function utf16to8(str) {
    var out, i, len, c;
    out = '';
    len = str.length;
    for (i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        }
        else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
        else {
            out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
    }
    return out;
}
/**
 * utf8 to utf16
 */
function utf8to16(str) {
    var out, i, len, c;
    var char2, char3;
    out = '';
    len = str.length;
    i = 0;
    while (i < len) {
        c = str.charCodeAt(i++);
        switch (c >> 4) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                // 0xxxxxxx
                out += str.charAt(i - 1);
                break;
            case 12:
            case 13:
                // 110x xxxx 10xx xxxx
                char2 = str.charCodeAt(i++);
                out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                break;
            case 14:
                // 1110 xxxx10xx xxxx10xx xxxx
                char2 = str.charCodeAt(i++);
                char3 = str.charCodeAt(i++);
                out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
                break;
        }
    }
    return out;
}
function int2char(n) {
    return '0123456789abcdefghijklmnopqrstuvwxyz'.charAt(n);
}
// convert a base64 string to hex
function b64tohex(s) {
    var ret = '';
    var i;
    var k = 0; // b64 state, 0-3
    var slop;
    for (i = 0; i < s.length; ++i) {
        if (s.charAt(i) == '=')
            break;
        var v = b64map.indexOf(s.charAt(i));
        if (v < 0)
            continue;
        if (k == 0) {
            ret += int2char(v >> 2);
            slop = v & 3;
            k = 1;
        }
        else if (k == 1) {
            ret += int2char((slop << 2) | (v >> 4));
            slop = v & 0xf;
            k = 2;
        }
        else if (k == 2) {
            ret += int2char(slop);
            ret += int2char(v >> 2);
            slop = v & 3;
            k = 3;
        }
        else {
            ret += int2char((slop << 2) | (v >> 4));
            ret += int2char(v & 0xf);
            k = 0;
        }
    }
    if (k == 1) {
        ret += int2char(slop << 2);
    }
    return ret;
}
// convert a base64 string to a byte/number array
function b64toBA(s) {
    var h = b64tohex(s);
    var i;
    var a = [];
    for (i = 0; 2 * i < h.length; ++i) {
        a[i] = parseInt(h.substring(2 * i, 2 * i + 2), 16);
    }
    return a;
}
var base64 = {
    encode: function (value) {
        return _base64.encode(utf16to8(value));
    },
    decode: function (value) {
        return utf8to16(_base64.decode(value));
    },
    BAtob64: function (bytes) {
        var i, len = bytes.length, base64 = '';
        if (bytes.get) {
            for (i = 0; i < len; i += 3) {
                base64 += b64map[bytes.get(i) >> 2];
                base64 += b64map[((bytes.get(i) & 3) << 4) | (bytes.get(i + 1) >> 4)];
                base64 += b64map[((bytes.get(i + 1) & 15) << 2) | (bytes.get(i + 2) >> 6)];
                base64 += b64map[bytes.get(i + 2) & 63];
            }
        }
        else {
            for (i = 0; i < len; i += 3) {
                base64 += b64map[bytes[i] >> 2];
                base64 += b64map[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
                base64 += b64map[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
                base64 += b64map[bytes[i + 2] & 63];
            }
        }
        if ((len % 3) === 2) {
            base64 = base64.substring(0, base64.length - 1) + '=';
        }
        else if (len % 3 === 1) {
            base64 = base64.substring(0, base64.length - 2) + '==';
        }
        return base64;
    },
    b64toBA: function (s) {
        return new Uint8Array(b64toBA(s));
    }
};
exports.default = base64;


//# sourceURL=webpack://MTDX/./src/core/utils/base64.ts?